﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Data;

public partial class InternalReceipts : System.Web.UI.Page
{
    int arrives;
    int noarrives;
    double arriveprices;
    double noarriveprices;
    protected void Page_Load(object sender, EventArgs e)
    {  //3918665
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["internal"] != null)
            {

                ViewState["mypro"] = Request.QueryString["internal"];
                
                //details.Visible = true;
                DataTable ds = ((DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty)).Table;
                string resum = ds.Compute("sum(actualcopyno)", "").ToString();
                arrive.Text = resum;
                string alls = ds.Compute("sum(assumedcopyno)", "").ToString();
                all.Text = alls;
                noarrive.Text = (int.Parse(alls) - int.Parse(resum)).ToString();
       
            }

            ViewState["filter"] = "";
            ViewState["from"] = "";
        }

    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                Thread.CurrentThread.CurrentCulture.DateTimeFormat = new CultureInfo("en-US", false).DateTimeFormat;
                // Thread.CurrentThread.CurrentCulture.Calendar=new  System.Globalization.GregorianCalendar();

            }
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    e.Row.Attributes.Add("onmouseover", "this.Mystyle=this.style.backgroundColor;this.style.backgroundColor='#FFFFA8'");

        //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.Mystyle");
        //}
    }
    protected void save_Click(object sender, EventArgs e)
    { string filter="";
    string commands = SqlDataSource1.SelectCommand;//.Split(new string[] { "union" }, StringSplitOptions.None);

        if ((DatePicker1.SelectedDate.Year.ToString() != "1") && (DatePicker2.SelectedDate.Year.ToString() != "1")) //&& (RadioButtonList1.SelectedValue != null))
        {
            string froms = DatePicker1.SelectedDate.ToString("yyyy-MM-dd");
            ViewState["from"] = froms;

            filter += " and  i.receiveddate between Cast('" + froms + "' AS datetime) and Cast('" + DatePicker2.SelectedDate.ToString("yyyy-MM-dd") + "' AS datetime)";
           
           
        }
        if (CheckBoxList1.SelectedItem!=null)
        {
            string ids = "";
            foreach (ListItem lst in CheckBoxList1.Items)
            {
                if (lst.Selected)
                {
                    ids += lst.Value + ",";
                }
            }
           ids= ids.TrimEnd(',');

           filter += "  and t.id in(" + ids + ")";
        }
        
        ViewState["filter"] = filter;
        SqlDataSource1.SelectCommand += filter;
        GridView1.DataBind();
        DataTable ds = ((DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty)).Table;
        string resum = ds.Compute("sum(actualcopyno)", "").ToString();
        arrive.Text = resum;
        string alls = ds.Compute("sum(assumedcopyno)", "").ToString();
        all.Text = alls;
        noarrive.Text = (int.Parse(alls) - int.Parse(resum)).ToString();
       
       
        
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        
    }
    public void adjust()
    {
        List<string> param = new List<string>();
        param.Add("@id," + ViewState["mypro"].ToString());
        param.Add("@intid," + ViewState["myid"].ToString());


        DataSet ds = DB.getproc("getstatsinternal", param);
        DataRow dr = ds.Tables[0].Rows[0];

        DataRow dr2 = ds.Tables[0].Rows[1];
        arrive.Text = dr[0].ToString();
        //arriveprice.Text = dr2["total"].ToString();
        noarrive.Text = dr2[0].ToString();
       // noarriveprice.Text = dr["total"].ToString();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string redirect = "";
        Session["filter"] = ViewState["filter"].ToString();
        //CustomerReceipt getIntReceipt = DB.getIntReceipt(ViewState["filter"].ToString(), ViewState["mypro"].ToString());
        if(ViewState["filter"].ToString().Contains("between"))
        {
            string froms = DatePicker1.SelectedDate.ToString("yyyy-MM-dd");
            string tos = DatePicker2.SelectedDate.ToString("yyyy-MM-dd");

            redirect = "~/reps2.aspx?rep=" + "CustomerReceipts2" + "&intid=" + ViewState["mypro"].ToString() + "&from=" + froms + "&to=" + tos;
        }
        else
        {
            redirect = "~/reps2.aspx?rep=" + "CustomerReceipts2" + "&intid=" + ViewState["mypro"].ToString();

        }
        Response.Redirect(redirect);
        //string redirect="";
        //if (ViewState["filter"].ToString() == "")
        //{
        //    Session["filter"] = SqlDataSource1.SelectCommand;
        //    redirect = "~/reps2.aspx?rep=" + "InternalHisRep" + "&id=" + ViewState["mypro"].ToString() + "&intid=" + ViewState["myid"].ToString();

        //}
        //else
        //{
        //    Session["filter"]=ViewState["filter"].ToString();
        //    if(ViewState["filter"].ToString().Contains("between"))
        //    {
        //    string froms = DatePicker1.SelectedDate.ToString("yyyy-MM-dd");
        //    string tos = DatePicker2.SelectedDate.ToString("yyyy-MM-dd");

        //    redirect = "~/reps2.aspx?rep=" + "InternalHisRep" + "&id=" + ViewState["mypro"].ToString() + "&intid=" + ViewState["myid"].ToString() + "&from=" +froms + "&to=" +tos;
        //    }
        //    else
        //    {
        //    redirect = "~/reps2.aspx?rep=" + "InternalHisRep" + "&id=" + ViewState["mypro"].ToString() + "&intid=" + ViewState["myid"].ToString();
            
        //    }

        //   // InternalHistories ints = DB.getIntHistoryrep(ViewState["filter"].ToString(), ViewState["mypro"].ToString(), ViewState["myid"].ToString());
        
        
        //}
        
        //Response.Redirect(redirect);
        //if(GridView1.Rows.Count>0)
        //{
        //    GridViewRow grv = GridView1.Rows[1];
        //    string dd = ((HiddenField)grv.FindControl("proid")).Value;
        //    if(ViewState["from"].ToString()=="")
        //      ViewState["from"]  = DateTime.Parse(((HiddenField)grv.FindControl("start")).Value).ToShortDateString();
            
        //   // string[] st = new string[] { "where" };
        //    string vv = ViewState["filter"].ToString() + " and t.id=" + ViewState["myid"].ToString(); //Request.QueryString["company_product"];//SqlDataSource1.SelectCommand.ToString().Split(st,StringSplitOptions.None)[0];
        //    CheckingHistory ch = DB.getCheckHistory(vv,dd);
        //    Session["thats"] = ch;
           
        //    //string myval = arrive.Text + "," + arriveprice.Text + "," + noarrive.Text + "," + noarriveprice.Text;
        //    //string redirect = "~/reps2.aspx?rep=" + "Checkings" + "&params=" + myval + "&from=" + ViewState["from"].ToString();
        //    //Response.Redirect(redirect);

        //}

    }

    //protected void currentsubs_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (currentsubs.SelectedValue != "0")
    //    {
    //        SqlDataSource1.SelectParameters["id"].DefaultValue = ViewState["mypro"].ToString();
    //        SqlDataSource1.SelectParameters["intid"].DefaultValue = currentsubs.SelectedValue;
    //        GridView1.DataBind();
    //        ViewState["myid"] = currentsubs.SelectedValue;
    //        adjust();
    //        details.Visible = true;
    //    }
    //}
}
